package com.bdmc.smokesensor.controller;


import java.util.Map;

import com.bdmc.smokesensor.Utils.Msg;
import com.bdmc.smokesensor.Utils.ResultUtil;
import com.bdmc.smokesensor.Utils.http.CTAPI;
import com.iotplatform.client.dto.InvokeDeviceServiceOutDTO;
import com.iotplatform.client.dto.QueryBatchSubOutDTO;
import com.iotplatform.client.dto.SubscriptionDTO;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping(value = "/ct")
public class CTController extends BaseController {


   @PostMapping(value = "/login")
    public Msg ctmsg(@RequestBody(required=false) String json) {
        
        String re = CTAPI.getAccessToken();
        
        return ResultUtil.success(re);
    }

    @PostMapping(value = "/query_sub")
    public Msg query_sub(@RequestBody(required=false) String json) {
        String token = CTAPI.getAccessToken();
        QueryBatchSubOutDTO re = CTAPI.getAllSubs(token);
        
        return ResultUtil.success(re);
    }

    @PostMapping(value = "/query_single_sub")
    public Msg query_single_sub(@RequestBody(required=false) Map<String, String> reqMap) {
        String sub_id =reqMap.get("sub_id").toString();
        String token = CTAPI.getAccessToken();
        SubscriptionDTO re = CTAPI.querySingleSub(sub_id, token);
        
        return ResultUtil.success(re);
    }

    @PostMapping(value = "/subscribe")
    public Msg subscribe(@RequestBody(required=false) Map<String, String> reqMap) {
        String api =reqMap.get("api").toString();
        String token = CTAPI.getAccessToken();
        SubscriptionDTO re = CTAPI.SubscribeDeviceBussiness(api,token);
        return ResultUtil.success(re);
    }

    @PostMapping(value = "/send")
    public Msg send(@RequestBody(required=false) Map<String, String> reqMap) {
        String id =reqMap.get("id").toString();
        String token = CTAPI.getAccessToken();
        InvokeDeviceServiceOutDTO re = CTAPI.sendXY(id,"a87c857d32af9266b4ab3a7884ecab8");
        return ResultUtil.success(re);
    }
}